<?php

class download 
{
    function download()
    {
    }
    
    function validateDownload()
    {
    	$msg = "";
    	if(trim($_POST['download_name']) == "")
    	{
    		$msg = _("Download name could not be empty");
    	}
    	

    	return $msg;
    }
    
    function deleteDownload($db, $id)
    {
    	//Delete Banner
    	$sql = sprintf("DELETE FROM %1\$s" .
				" where %1\$s.download_id = '$id'", 
				TBL_DOWNLOAD);
		
    	$res = $db->query($sql);
  	
    	return $res;
    }
    
    function saveDownload($db, $download)
    {
    	$lang = $_SESSION['LC_ALL'];
    	$res = $db->query(sprintf(
					"INSERT INTO %s(download_name, product_id, file, lang_id,wheretoorder) " .
					"VALUES('%s', '%s', '%s', '%s','%s')",
    				TBL_DOWNLOAD, 
    				mysql_real_escape_string($download['download_name']),
    				$download['product_id'],
    				$download['file'],
    				$lang,
					$download['where_id']));
    	
    	if($res == true)
    		return $db->insert_id;
    	else
    		return -1;
    }
    
    function updateDownload($db, $download)
    {
    	
					
		$res =	$db->query(sprintf(
					"UPDATE %s SET " .
					"download_name	   = '$download[download_name]', " .
					"product_id 	   = '$download[product_id]', " .
					"file 	           = '$download[file]', " .
					"wheretoorder	   = '$download[where_id]' " . 
					"WHERE download_id = '$download[download_id]'",
    				TBL_DOWNLOAD)); 
    	
    	return $res;
    }
    
    function getDownloadById($db, $id) {	
		$langid = $_SESSION["LC_ALL"];
    	$sql = sprintf("SELECT d.* FROM ".TBL_DOWNLOAD." d ".
						" WHERE d.download_id='$id'");
    	return $db->get_row($sql);
    }
    

    function countgetAllDownload($db,$lang_id ,$type){
    	$sql = sprintf("SELECT count(d.download_id) as total FROM ".TBL_DOWNLOAD." d ".
						" LEFT JOIN ".TBL_PRODUCT." p ON p.product_id = d.product_id ".
						" LEFT JOIN ".TBL_PRODUCT_LANG." l ON l.product_id = p.product_id AND l.lang_id = '$langid' ".
						"WHERE d.lang_id = '$lang_id'");
		$res = 	$db->get_row($sql);		
    	return $res->total;
    }
	
	function getAllDownloadShare($db,$langid,$maxview,$path ,$type){
		$page = isset ( $_GET[$path] ) ? intval ( $_GET[$path] ) : 1;
		$rows_per_page= $maxview;
		$page_start = ( $page - 1 ) * $rows_per_page;
		$page_end = $page * $rows_per_page;
    	$sql = sprintf("SELECT d.*,p.product_image,l.product_name  FROM ".TBL_DOWNLOAD." d ".
						" LEFT JOIN ".TBL_PRODUCT." p ON p.product_id = d.product_id ".
						" LEFT JOIN ".TBL_PRODUCT_LANG." l ON l.product_id = p.product_id AND l.lang_id = '$langid' ".
						"WHERE d.lang_id = '$langid' LIMIT $page_start, $rows_per_page");
		$res = 	$db->get_results($sql);		
    	return $res;
    }
    
	function getAllDownloadLimit($db){
    	$lang = $_SESSION['LC_ALL'];
    	$sql = sprintf("SELECT * FROM ".TBL_DOWNLOAD." d LEFT JOIN ".TBL_PRODUCT." p ON d.product_id = p.product_id WHERE d.lang_id = '$lang' LIMIT 0,2");
    	return $db->get_results($sql);
    }
	
    function getDownloadForSearch($db, $name, $type)
    {
    	$lang = $_SESSION['LC_ALL'];
    	if($name != "")
    	{ 
	    	if($type == 0)
	    	{
	    		$cond = "LEFT JOIN ".TBL_CATEGORY." c ON p.category_id = c.cat_ID WHERE c.cat_name like '$name%%' OR p.product_name like '$name%%' AND d.lang_id = '$lang'";
	    	}
	    	else if($type == 1)
			{
	    		$cond = "LEFT JOIN ".TBL_CATEGORY." c ON p.category_id = c.cat_ID WHERE c.cat_name like '$name%%' AND d.lang_id = '$lang'";
			}
			else if($type == 2)
			{
	    		$cond = "WHERE p.product_name like '$name%%' AND d.lang_id = '$lang'";
			}
			$sql = sprintf("SELECT * FROM ".TBL_DOWNLOAD." d LEFT JOIN ".TBL_PRODUCT." p ON d.product_id = p.product_id $cond");
    	}
		else
		{
			$sql = sprintf("SELECT * FROM ".TBL_DOWNLOAD." d LEFT JOIN ".TBL_PRODUCT." p ON d.product_id = p.product_id WHERE d.lang_id = '$lang'");
		}
		
    	return $db->get_results($sql);
    }
    

}
?>